#include <stdio.h>

static char daytab[2][13] = {
       {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
       {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
   };

int month_day(int year, int yearday, int *pmonth, int *pday);
int day_of_year(int year, int month, int day);
void test_day_of_year();

int main()
{
  test_day_of_year();
  return(0);
}  

void test_day_of_year()
{
  for (int j=1990; j<2019; j++)
    for(int k=1; k<13; k++)
      for(int i=1; i<32; i++)
        printf("day_of_year %d \n",day_of_year(j,k,i));
}


   /* day_of_year:  set day of year from month & day */
   int day_of_year(int year, int month, int day)
   {
       int i, leap;
       
       if (month<1)
         return -1;
       if (month>12)
         return -2;

       leap = ((year%4) == 0) && ((year%100) != 0) || ((year%400) == 0);

       if (day > daytab[leap][month])
         return -4;
       if (day <0)
         return -8;
       if(year<0)
         return -16;
       
       for (i = 1; i < month; i++)
           day += daytab[leap][i];
       return day;
   }

   /* month_day:  set month, day from day of year */
   int month_day(int year, int yearday, int *pmonth, int *pday)
   {
       int i, leap;
       if (year<0) return -16;
       if (yearday>366) return -1;
       if (yearday<1) return -2;
       
       leap = ((year%4) == 0) && ((year%100) != 0) || ((year%400) == 0);
       if ((!leap) && (yearday==366)) return -4;
       
       for (i = 1; yearday > daytab[leap][i]; i++)
         yearday -= daytab[leap][i];
       
       *pmonth = i;
       *pday = yearday;
   }
